package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.OrdersManual;
import com.ruoyi.system.mapper.OrdersManualMapper;
import com.ruoyi.system.service.IOrdersManualService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 车副计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class OrdersManualServiceImpl implements IOrdersManualService
{
    private static final Logger log = LoggerFactory.getLogger(OrdersManualServiceImpl.class);

    @Autowired
    private OrdersManualMapper ordersManualMapper;

    /**
     * 查询车副计划
     * 
     * @param id 车副计划主键
     * @return 车副计划
     */
    @Override
    public OrdersManual selectOrdersManualById(Long id)
    {
        return ordersManualMapper.selectOrdersManualById(id);
    }

    /**
     * 查询车副计划
     *
     * @param code 车副计划编码
     * @return 车副计划
     */
    public OrdersManual selectOrdersManualByCode(String code)
    {
        return ordersManualMapper.selectOrdersManualByCode(code);
    }

    /**
     * 查询车副计划列表
     * 
     * @param ordersManual 车副计划
     * @return 车副计划
     */
    @Override
    public List<OrdersManual> selectOrdersManualList(OrdersManual ordersManual)
    {
        return ordersManualMapper.selectOrdersManualList(ordersManual);
    }

    /**
     * 新增车副计划
     * 
     * @param ordersManual 车副计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOrdersManual(OrdersManual ordersManual)
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNowStr = sdf.format(d);

        ordersManual.setCreateTime(DateUtils.getNowDate());
        ordersManual.setCreateBy(SecurityUtils.getUsername());
        ordersManual.setStatus(UserConstants.NORMAL);
        //获取最新编号
        OrdersManual om = ordersManualMapper.findNumber();
        String sortNo = "";
        String markNo = "";
        if(om != null){
            String sdate = om.getSortid().substring(14,22);
            String mdate = om.getMarkid().substring(14,22);
            if(sdate.equals(dateNowStr)){
                sortNo = om.getSortid().substring(22);
            }
            if(mdate.equals(dateNowStr)){
                markNo = om.getMarkid().substring(22);
            }
        }
        String sno = NumberUtil.getNewEquipmentNo(Constants.ProductManualO,dateNowStr,sortNo);
        String mno = NumberUtil.getNewEquipmentNo(Constants.ProductManualM,dateNowStr,markNo);
        ordersManual.setSortid(sno);
        ordersManual.setMarkid(mno);
        return ordersManualMapper.insertOrdersManual(ordersManual);
    }

    /**
     * 修改车副计划
     * 
     * @param ordersManual 车副计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOrdersManual(OrdersManual ordersManual)
    {
        ordersManual.setUpdateTime(DateUtils.getNowDate());
        ordersManual.setUpdateBy(SecurityUtils.getUsername());
        return ordersManualMapper.updateOrdersManual(ordersManual);
    }

    /**
     * 批量删除车副计划
     *
     * @param ids 需要删除的车副计划主键
     * @return 结果
     */
    @Override
    public int deleteOrdersManualByIds(Long[] ids) {
        return ordersManualMapper.deleteOrdersManualByIds(ids);
    }

    /**
     * 删除车副计划信息
     * 
     * @param id 车副计划主键
     * @return 结果
     */
    @Override
    public int deleteOrdersManualById(Long id)
    {
        return ordersManualMapper.deleteOrdersManualById(id);
    }

    /**
     * 导入车副计划数据
     *
     * @param ordersManualList 车副计划数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importOrdersManual(List<OrdersManual> ordersManualList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(ordersManualList) || ordersManualList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (OrdersManual ordersManual : ordersManualList)
        {
            try
            {
                // 验证是否存在这个车副计划
                OrdersManual sa = ordersManualMapper.selectOrdersManualByCode(ordersManual.getModelCode());
                if (StringUtils.isNull(sa))
                {
                    ordersManual.setCreateBy(operName);
                    this.insertOrdersManual(ordersManual);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、车副计划 " + ordersManual.getModel() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    ordersManual.setUpdateBy(operName);
                    this.updateOrdersManual(ordersManual);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、车副计划 " + ordersManual.getModel() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、车副计划 " + ordersManual.getModel() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、车副计划 " + ordersManual.getModel() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
